package com.collectplus.express.parcel;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.MainActivity;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.DialogMgr;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.CollectingPassportBean;

import droid.frame.view.xlist.SListView;

/**
 * Created by lidandan on 2018/3/7.
 * 代收------待取走的包裹
 */
public class ParcelWaitTakeActivity extends BaseActivity2 {

    private SListView listView;
    private CollectingPassportBean item;
    private TextView parcelNumTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        item = (CollectingPassportBean) getIntent().getSerializableExtra("item");
        super.onCreate(savedInstanceState);
        if (item != null) {
            AppHttp.getInstance().getCollectingParcelList(item.getStationId(), item.getStatus(), item.getId());
        } else {
            showToast("数据有误");
        }
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.parcel_wait_take_activity);
        super.findViewById();
        getAppTitle().setCommonTitle("待取走的包裹");
        listView = (SListView) findViewById(R.id.listView);
        parcelNumTv = (TextView) findViewById(R.id.parcel_num_tv);
        findViewById(R.id.confirm_take_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogMgr.confirmTake(ParcelWaitTakeActivity.this, item.getTotal());
            }
        });
        parcelNumTv.setText("共有" + item.getTotal() + "个包裹在该店");

    }

    /**
     * 确认取件方法
     */
    public void pcicUpParcel() {
        if (item != null) {
            showLoadingDialog(null);
            AppHttp.getInstance().confirmCollectingParcelList(item.getStationId(), item.getStatus());
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.get_collecting_parcel_list:
                cancelLoadingDialog();
                final AppResult<CollectingPassportBean> parcelResult = JSON.parseList(msg.obj, CollectingPassportBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (parcelResult.isSuccess()) {
                            ParcelWaitTakeAdapter adapter = new ParcelWaitTakeAdapter(parcelResult.getResultList(), getContext());
                            listView.setAdapter(adapter);
                            parcelNumTv.setText("共有" + adapter.getItemsSize() + "个包裹在该店");
                        } else {
                            showToast("获取数据失败");
                        }
                    }
                });
                return true;
            //确认取走包裹
            case MsgID.confirm_collecting_parcel_list:
                cancelLoadingDialog();
                AppResult<?> result = JSON.parser(msg.obj);
                if (result.isSuccess()) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    Bundle bundle = new Bundle();
                    bundle.putString("stationId", item.getStationId());
                    bundle.putString("takeId", item.getTakeId());
                    intent.putExtras(bundle);
                    startActivity(intent);
                    finish();
                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }

    }

}
